package com.sayice.robotconflict.game.handler;

import com.alibaba.fastjson2.JSONObject;
import com.sayice.robotconflict.game.Game;
import com.sayice.robotconflict.game.msg.Message;
import com.sayice.robotconflict.game.msg.data.send.TeamInviteInfo;
import com.sayice.robotconflict.game.object.Player;
import com.sayice.robotconflict.game.object.TeamRoom;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

/**
 * 组队处理器
 *
 * @author shubing
 */
@Slf4j
public class TeamHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        Object[] playerAndMsg = (Object[]) msg;
        Player player = (Player) playerAndMsg[0];
        Message message = (Message) playerAndMsg[1];
        if ("team".equals(message.getType())) {
            switch (message.getAction()) {
                // 创建组队房间
                case "create": {
                    if (player.state.get() != Player.State.IDLE) {
                        break;
                    }
                    int maxSize = (int) message.getData();
                    TeamRoom.createRoom(maxSize, player);
                    break;
                }
                // 邀请加入组队房间
                case "invite": {
                    if (player.state.get() != Player.State.TEAMING) {
                        break;
                    }
                    TeamInviteInfo inviteInfo = ((JSONObject) message.getData()).to(TeamInviteInfo.class);
                    Player target = Game.PLAYER_GROUP.find(inviteInfo.getPlayerId());
                    if (target != null) {
                        inviteInfo.setPlayerId(player.id);
                        inviteInfo.setPlayerName(player.name);
                        target.send(new Message("team", "invite", inviteInfo));
                    }
                    break;
                }
                // 加入组队房间
                case "join": {
                    if (player.state.get() != Player.State.IDLE) {
                        break;
                    }
                    int roomId = (int) message.getData();
                    TeamRoom teamRoom = Game.TEAM_ROOM_MAP.get(roomId);
                    if (teamRoom == null || !teamRoom.addPlayer(player)) {
                        player.send(new Message("msg", null, "加入失败，房间可能已关闭"));
                    }
                    break;
                }
                // 离开组队房间
                case "leave": {
                    if (player.state.get() != Player.State.TEAMING) {
                        break;
                    }
                    int roomId = (int) message.getData();
                    TeamRoom teamRoom = Game.TEAM_ROOM_MAP.get(roomId);
                    if (teamRoom != null) {
                        teamRoom.removePlayer(player);
                    }
                    break;
                }
                // 准备
                case "ready": {
                    if (player.state.get() != Player.State.TEAMING) {
                        break;
                    }
                    int roomId = (int) message.getData();
                    TeamRoom teamRoom = Game.TEAM_ROOM_MAP.get(roomId);
                    if (teamRoom != null) {
                        teamRoom.setReady(player, true);
                    }
                    break;
                }
                // 取消准备
                case "unready": {
                    if (player.state.get() != Player.State.TEAMING) {
                        break;
                    }
                    int roomId = (int) message.getData();
                    TeamRoom teamRoom = Game.TEAM_ROOM_MAP.get(roomId);
                    if (teamRoom != null) {
                        teamRoom.setReady(player, false);
                    }
                    break;
                }
                default:
                    break;
            }
        } else {
            ctx.fireChannelRead(playerAndMsg);
        }
    }
    
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        log.error("NettyHandler Exception from " + ctx.channel().remoteAddress(), cause);
        ctx.channel().close();
    }
}
